#!/usr/bin/python3
#coding=utf8
import threading
import time
import socket #引入模块，socket
import traceback
import queue


servicelist=['192.168.199.102','192.168.199.103','192.168.199.104','192.168.199.105','192.168.199.106']
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(0.5)

def connect(serviceaddr,port):
	try:
		s.connect((serviceaddr,port))
		ans=s.recv(1024)
		poem = serviceaddr+':'+str(port)
		if poem:
			return poem
	except:
		pass

def process_data(threadName, q):
	while not exitFlag:
		queueLock.acquire()
		if not workQueue.empty():
			data = q.get()
			queueLock.release()
			print ("%s processing %s" % (threadName, data))
		else:
			queueLock.release()
		time.sleep(1)

class myThread (threading.Thread):
	def __init__(self, serviceaddr,port):
		threading.Thread.__init__(self)
		self.serviceaddr = serviceaddr
		self.port = port
	def run(self):
		print ("开启线程： " + self.serviceaddr+ ": "+str( self.port ) )
	        # print_time(self.name, self.counter, 3)
		s=connect( self.serviceaddr, self.port )
		if s:
			f=open('connect1.txt','a+')
			f.write(s+"\n")
			f.close()
	        # 释放锁，开启下一个线程
		print ("退出线程：" + self.serviceaddr+":"+str( self.port ) )
		

threadLock = threading.Lock()
threads = []
workQueue = queue.Queue(10)

for slist in servicelist:
# 	print(slist)
	for port in range(1,65535):
		var	= None
		var = myThread(slist,port)
		var.start()
		threads.append(var)

# 获取锁，用于线程同步
threadLock.acquire()
for word in threads:
	workQueue.put(word)
# 释放锁，开启下一个线程
threadLock.release()


while not workQueue.empty():
	pass

for t in threads:
	t.join()

print('退出进程')

